<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="保障责任编码" prop="categoryCode">
        <el-select 
          v-model="queryParams.categoryCode"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option v-for="item in categoryCodeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="公式编码" prop="suggestFormula">
        <el-select 
          v-model="queryParams.suggestFormula"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option v-for="item in suggestFormulaOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">查询</el-button>
        <el-button  @click="addData">添加</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="国别代码" prop="countryCode"></el-table-column>
      <el-table-column label="区域代码" prop="areaCode"></el-table-column>
      <el-table-column label="保障责任编码" prop="categoryCode"></el-table-column>
      <el-table-column label="公式编码" prop="suggestFormula"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-tooltip content="修改" placement="top">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row, 'edit')" ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top">
            <el-button link type="danger" icon="Delete" ></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog v-model="dialogFormVisible" :title="handleType ==='edit'?'修改关联关系': '新建关联关系'" width="500">
      <el-form :model="dialogForm">
        <el-form-item label="国别代码" :label-width="formLabelWidth">
          <el-select v-model="dialogForm.countryCode" placeholder="请选择">
              <el-option :key="156" :label="中国" :value="156" />
            </el-select>
        </el-form-item>
        <el-form-item label="区域代码" :label-width="formLabelWidth">
          <el-select v-model="dialogForm.areaCode" placeholder="请选择">
            <el-option :key="W" :label="W" :value="W" />
            </el-select>
        </el-form-item>
        <el-form-item label="保障责任编码" :label-width="formLabelWidth">
          <el-select v-model="dialogForm.categoryCode" placeholder="请选择">
              <el-option v-for="item in categoryCodeOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item>
        <el-form-item label="公式编码" :label-width="formLabelWidth">
          <el-select v-model="dialogForm.suggestFormula" placeholder="请选择">
              <el-option v-for="item in suggestFormulaOptions" :key="item.value" :label="item.label" :value="item.value" />
            </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave">
            保存
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="CorrelationFormula">
import { formulaFilterQuery, formulaFilterSave, categoryQuery, ruleQuery } from '@/api/safeguard/index.js';
const queryParams = ref({
  categoryCode: '',
  suggestFormula: '',
})

const handleType = ref('')
const showSearch = ref(true);
const loading = ref(false);
const tableData = ref([]);

const dialogFormVisible = ref(false)
const dialogForm = ref({
  filterId: '',
  countryCode: '156',
  areaCode: 'W',
  categoryCode: '',
  suggestFormula: '',
})
const formLabelWidth = '140px'
const categoryCodeOptions = ref([])
const suggestFormulaOptions = ref([])

function getList () {
  loading.value = true;
  formulaFilterQuery('5').then(res => {
    tableData.value = res.data || []
  }).finally(() => {
    loading.value = false;
  })
}

function handleQuery() {
  getList();
}

 /** 添加 */
 function addData () {
  dialogForm.value = {
    filterId: '',
    countryCode: '156',
    areaCode: 'W',
    categoryCode: '',
    suggestFormula: '',
  }
  dialogFormVisible.value = true
}
 /** 编辑 */
function handleUpdate (row, type) {
  handleType.value = type
  dialogForm.value = row
  dialogFormVisible.value = true
}

function handleSave() {  
  // 保存成功刷新数据
  formulaFilterSave({...dialogForm.value}).then(() => {
    dialogFormVisible.value = false
    getList();
  })
}
function getDutyData () {
  const userId = '4'
  categoryCodeOptions.value = []
  categoryQuery(userId).then(res => {
    res.data.map(item => {
      categoryCodeOptions.value.push({
        value: item.categoryCode,
        label: item.categoryCode + '-'+ item.categoryDesc,
      })
    })
  })
}
function getRuleData () {
  const userId = '5'
  suggestFormulaOptions.value = []
  ruleQuery(userId).then(res => {
    res.data.map(item => {
      suggestFormulaOptions.value.push({
        value: item.formulaCode,
        label: item.formulaCode,
      })
    })
  })
}
getRuleData ();
getDutyData ()
handleQuery();
</script>
